//
//  DSSettingViewController.m
//  DSJY
//
//  Created by lx on 17/7/14.
//  Copyright © 2017年 dsjy. All rights reserved.
//  设置VC

#import "DSSettingViewController.h"
#import "DSSettingViewModel.h"
#import "DSAccountViewModel.h"
#import "NSString+Extension.h"
#import "DSAppResAddressIp.h"
#import "NSString+Extension.h"

@interface DSSettingViewController ()
//是否分享到排行榜显示，默认是
@property (weak, nonatomic) IBOutlet UISwitch *rankingButton;
//是否上传成绩，默认否
@property (weak, nonatomic) IBOutlet UISwitch *uploadScoreButton;
//设置VM
@property (nonatomic, strong) DSSettingViewModel *settingVM;
//线路1
@property (weak, nonatomic) IBOutlet UILabel *lineLabel1;
@property (weak, nonatomic) IBOutlet UISwitch *lineSwitch1;
//线路2
@property (weak, nonatomic) IBOutlet UILabel *lineLabel2;
@property (weak, nonatomic) IBOutlet UISwitch *lineSwitch2;
//线路3
@property (weak, nonatomic) IBOutlet UILabel *lineLabel3;
@property (weak, nonatomic) IBOutlet UISwitch *lineSwitch3;
//默认的switch
@property (nonatomic, weak) UISwitch *defaultSwithch;
//线控件集合
@property (nonatomic, strong) NSArray *lineControls;
@end

@implementation DSSettingViewController

#pragma mark - 懒加载
- (DSSettingViewModel *)settingVM
{
    if (_settingVM == nil) {
        _settingVM = [[DSSettingViewModel alloc] init];
    }
    return _settingVM;
}

- (NSArray *)lineControls
{
    if (_lineControls == nil) {
        _lineControls = @[self.lineSwitch1, self.lineSwitch2, self.lineSwitch3];
    }
    return _lineControls;
}

- (UISwitch *)defaultSwithch
{
    NSArray *appIds = [DSAppResAddressIp sharedInstance].appIds;
    if (appIds.count >=3) {
        return self.lineSwitch3;
    }else if(appIds.count >=2){
        return self.lineSwitch2;
    }else{
        return self.lineSwitch1;
    }
}

#pragma mark - 系统回调
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //初始化UI
    [self setupUI];
    //绑定事件
    [self bindEvent];
}

- (void)setupUI
{
    self.showBackItem = YES;
    //获取用户的分享状态
    DSAccount *account = [DSAccountViewModel sharedAccount].account;
    [self.rankingButton setOn:account.isSharescore];
    
    //设置用户的上传成绩状态
    [self.uploadScoreButton setOn:[DSAccountViewModel sharedAccount].account.isUploadScore];
    
    //线路
    [self setupLine];
}

- (void)setupLine
{
    [self lineControls];
    NSArray *appIds = [DSAppResAddressIp sharedInstance].appIds;
    if (appIds != nil && appIds.count > 0) {
        for (NSInteger i = 0; i<appIds.count; i++) {
            NSString *lineText = [NSString stringWithFormat:@"线路%ld", appIds.count - i];
            if (i == 0) {
                [self setupLineLabel:self.lineLabel1 lineSwitch:self.lineSwitch1 appId:appIds[i] lineText:lineText];
            }else if(i == 1){
                [self setupLineLabel:self.lineLabel2 lineSwitch:self.lineSwitch2 appId:appIds[i] lineText:lineText];
            }else if(i == 2){
                [self setupLineLabel:self.lineLabel3 lineSwitch:self.lineSwitch3 appId:appIds[i] lineText:lineText];
            }
        }
        if ([NSString isEmptyWithStr:[MTUserDefaults objectForKey:appIds_key]]) {
            self.defaultSwithch.on = YES;
        }
    }
}

- (void)setupLineLabel:(UILabel *)lineLabel lineSwitch:(UISwitch *)lineSwitch appId:(NSString *)appId lineText:(NSString *)lineText
{
    lineLabel.hidden = NO;
    lineSwitch.hidden = NO;
    lineLabel.text = lineText;
    if ([appId isEqualToString:[MTUserDefaults objectForKey:appIds_key]]) {
        [lineSwitch setOn:YES];
    }
}

#pragma mark - 绑定事件
- (void)bindEvent
{
    @weakify(self);
    [self.settingVM.updateRankingCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        @strongify(self);
        NSString *sharescore = self.rankingButton.isOn ? @"1" : @"0";
        [DSAccountViewModel sharedAccount].account.sharescore = sharescore;
        [[DSAccountViewModel sharedAccount] write];
    }];
}

//是否分享成绩到排行榜
- (IBAction)rankingButtonClick:(UISwitch *)sender
{
    //设置是否分享到排行榜显示
    NSString *sharescore = self.rankingButton.isOn ? @"1" : @"0";
    [self.settingVM.updateRankingCommand execute:sharescore];
}

//是否上传成绩
- (IBAction)uploadScoreButtonClick:(UISwitch *)sender
{
    [DSAccountViewModel sharedAccount].account.uploadScore = self.uploadScoreButton.isOn;
    [[DSAccountViewModel sharedAccount] write];
}

- (IBAction)changeLineSwitch1:(UISwitch *)sender
{
    [self chaneLineSwitch:sender];
}

- (void)chaneLineSwitch:(UISwitch *)sender
{
    sender.on = !sender.isOn;
    for (UISwitch *subView in self.lineControls) {
        if (subView != sender) {
            subView.on = NO;
        }
    }
    if (sender.isOn == NO) {
        self.defaultSwithch.on = YES;
    }
    for (UISwitch *subView in self.lineControls) {
        if (subView.isOn) {
            NSString *appId = [DSAppResAddressIp sharedInstance].appIds[subView.tag - 1];
            [MTUserDefaults setObject:appId forKey:appIds_key];
        }
    }
}

@end
